import numpy as np
import csv
import random
from datetime import datetime
from time import strftime


def getCurTime():
    """
    get current time
    Return value of the date string format(%Y-%m-%d %H:%M:%S)
    """
    format='%Y-%m-%d %H:%M:%S'
    sdate = None
    cdate = datetime.now()
    try:
        sdate = cdate.strftime(format)
    except:
        raise ValueError
    return sdate

def build_data_list(inputCSV):
    sKey = []
    fn = inputCSV
    ra = csv.DictReader(file(fn), dialect="excel")
    
    for record in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        for item in ra.fieldnames:
            temp = float(record[item])
            sKey.append(temp)
    sKey = np.array(sKey)
    sKey.shape=(-1,len(ra.fieldnames))
    return sKey

def build_data_list_withoutColumnName(inputCSV):
    sKey = []
    fn = inputCSV
    ra = csv.reader(file(fn), dialect="excel")
    i = 0
    for row in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        i += 1
        for item in row:
            temp = float(item)
            sKey.append(temp)
    #sKey.shape=(-1,i)
    sKey = np.array(sKey)
    sKey.shape = (i, -1)   
    return sKey
#--------------------------------------------------------------------------
#MAIN

if __name__ == "__main__":
    print "========================================"
    print "begin at " + getCurTime()
    
    #idCSV = 'C:/_DATA/CancerData/SatScan/neast_id.csv'
    #dataMatrix = build_data_list_withoutColumnName(idCSV)
    #id = dataMatrix[:,0]
    caseCSV = 'C:/_DATA/CancerData/SatScan/mult6000/three16_modify/three16_format_modify.csv'
    dataMatrix = build_data_list(caseCSV)
    iLen = dataMatrix.shape
    print dataMatrix, iLen
    
    output = np.zeros((iLen[0], 2))
    output[:,0] = dataMatrix[:,0]
    output[:,1] = dataMatrix[:,1]
    print output
    #np.savetxt(caseCSV[:-3]+'pop', output, delimiter='\t', fmt='%i')
    #for i in range(0, 0):
    '''
    for i in range(2, iLen[1]):
        output[:,1] = dataMatrix[:,i]
        filename = 'C:/_DATA/CancerData/SatScan/mult6000/three16_modify/satscan/'+str(i-2)+'.cas'
        np.savetxt(filename, output, delimiter='\t', fmt='%i')
    print caseCSV
    print "end at " + getCurTime()


'''